package com.gmscosmo.janus.util;

import com.gmscosmo.janus.config.JanusConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class JanusLogUtil {
    public static final String DEBUG = "DEBUG";
    public static final String INFO = "INFO";
    public static final String WARN = "WARN";
    public static final String ERROR = "ERROR";
    public static final String TRACE = "TRACE";
    private static ConcurrentHashMap<String, Logger> loggers = new ConcurrentHashMap<>(16);
    @Autowired
    JanusConfig janusConfig;

    private static Logger createLogger(Class clazz) {
        Logger LOG = LoggerFactory.getLogger(clazz);
        loggers.put(clazz.getName(), LOG);
        return LOG;
    }

    public void log(String msg, Class clazz) {
        if (janusConfig.isIs_log()) {
            Logger LOG = loggers.get(clazz.getName());
            if (LOG==null) {
                LOG = createLogger(clazz);
            }
            if(janusConfig.getLog_level()>=3) {
                LOG.info(msg);
            }
        }
    }

    public void log(String msg, Class clazz, String level) {
        if (janusConfig.isIs_log()) {
            Logger LOG = loggers.get(clazz.getName());
            if (LOG==null) {
                LOG = createLogger(clazz);
            }
            switch (level) {
                case "TRACE":
                    if(janusConfig.getLog_level()>=5) {
                        LOG.trace(msg);
                    }
                    break;
                case "DEBUG":
                    if(janusConfig.getLog_level()>=4) {
                        LOG.debug(msg);
                    }
                    break;
                case "INFO":
                    if(janusConfig.getLog_level()>=3) {
                        LOG.info(msg);
                    }
                    break;
                case "WARN":
                    if(janusConfig.getLog_level()>=2) {
                        LOG.warn(msg);
                    }
                    break;
                case "ERROR":
                    if(janusConfig.getLog_level()>=1) {
                        LOG.error(msg);
                    }
                    break;
                default:
                    LOG.error("日志级别错误");
                    break;
            }
        }
    }
}
